﻿using System.Windows;
using XAMLStylePad.Models;
using XAMLStylePad.ViewModels;
using XAMLStylePad.Views;
using System.Windows.Controls;
using System.ComponentModel;
using System;
using System.Reflection;
using FireFly;
using System.Diagnostics;

namespace XAMLStylePad
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class XAMLStylePadMainWindow : Window, IUIContext
    {
        #region Initialize
        public XAMLStylePadMainWindow()
        {
            InitializeComponent();
            Init();
        }

        void Init()
        {
            ViewModelService.Instance = new XamlSPViewModelService();
            Instance = this;
            menu.ItemsSource = ViewModel.Menu;
#if DEBUG
            ViewModel.Project = new ProjectViewModel(TestProject);
#else
            ViewModel.Project = new ProjectViewModel(new AppProject());
            
#endif
        }


        void Items_submenu(object sender, RoutedEventArgs e)
        {
        }

        MenuItem CreateMenuItem(IRelayCommand cmd, IViewModel viewModel)
        {
            var item = new MenuItem()
            {
                Header = cmd.Caption,
                Command = cmd
            };
            
            item.CommandParameter = viewModel;
            return item;
        } 
        #endregion

        public static IUIContext Instance
        {
            get;
            private set;
        }

        public MainWindowViewModel ViewModel
        {
            get
            {
                if(_ViewModel == null)
                {
                    _ViewModel = new MainWindowViewModel(this);
                    _ViewModel.PropertyChanged += new PropertyChangedEventHandler(_ViewModel_PropertyChanged);
                }
                return _ViewModel;
            }
        }

        void _ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "Project")
            {
                tree.ItemsSource = new ProjectViewModel[] { ViewModel.Project };
            }
        }
        MainWindowViewModel _ViewModel;

        #region IUIContext Members

        public StylePadTree Tree
        {
            get
            {
                return tree;
            }
        }

        public PreviewPanel PreviewPanel
        {
            get
            {
                return preview;
            }
        }

        public XAMLEditor Editor
        {
            get
            {
                return txXaml;
            }
        }

        public void SetObject(object obj)
        {
            Editor.SetXAML(obj);
            if(obj == null)
                PreviewPanel.ResetObject();
            else if(obj is Style)
                PreviewPanel.SetStyle(obj as Style);
            //else if(obj is FrameworkTemplate)
            //    PreviewPanel.SetTemplate(obj as FrameworkTemplate);
            else if(obj is FrameworkElement)
                PreviewPanel.SetElement(obj as FrameworkElement);
        }
        #endregion

        public static ProjectBase TestProject
        {
            get
            {
                try
                {
                    if(_prj == null)
                    {
                        _prj = new AppProject()
                        {
                            Name = "test",
                            Directory = "c:\test.prj"
                        };

                        SourceBase src;


                        //src = new AssemblySource(Environment.CurrentDirectory + @"\Tests\StylingIntroSample.exe")
                        //{
                        //    Name = "StylingIntroSample.exe"
                        //};
                        src = new AssemblySource(Environment.CurrentDirectory + @"\Tests\SampleWpfApplication.exe")
                        {
                            Name = "SampleWpfApplication.exe"
                        };
                        _prj.Sources.Add(src);
                        src.Load();
                    }
                }
                catch(Exception err)
                {
                    Debug.WriteLine(err.Message + "\n" + err.StackTrace);
                }
                return _prj;
            }
        }
        static ProjectBase _prj;

        private void tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            ViewModel.CurrentItem = e.NewValue as IViewModel;
        }

        public MessageBoxResult ShowMessageBox(string message, string title, MessageBoxButton messageBoxButton, MessageBoxImage icon)
        {
            return MessageBox.Show(message, title, messageBoxButton, icon);
        }

        public void ShowMessageBox(string message)
        {
            MessageBox.Show(message);
        }
    }
}
